/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package extension.room.tan;

import extension.game.room.Player;
import java.util.LinkedList;
import java.util.List;

/**
 * @author daz
 */
public class TanPlayer extends Player {

    public static final int FINISH = 3;
    public static final int SKIP = 4;
    public static final int WAIT_ATTACK = 5;
    public static final int ATTACKING = 6;
    public static final int DEFENSE_LOST = 7;
    public static final int DEFENSE = 8;
    private int attackState;

    public TanPlayer(String name, long money, int seatPos) {
        super(name, money, seatPos);
    }

    @Override
    public void resetCard() {
        listCardIds.clear();
        numberCardInHands = 0;
        rank = 0;

    }

    public void addCard(int cardId) {
        listCardIds.add(cardId);
    }

    public void removeCardInHand(int cardRemove) {
        for (Integer cardId : listCardIds) {
            if (cardId.equals(cardRemove)) {
                listCardIds.remove(cardId);
                break;
            }
        }
        numberCardInHands = listCardIds.size();
    }

    void setRank(int currentRank) {
        this.rank = currentRank;
    }

    public void setAttackState(int attackState) {
        this.attackState = attackState;
    }

    public int getAttackState() {
        return attackState;
    }

    void addCard(List<Integer> listAdditonalCard) {
        listCardIds.addAll(listAdditonalCard);
    }

    public int getSuggestDefenseCard(int currentAttackCard, int masterCard) {
        int minCardId = -1;
        for (Integer cardId : listCardIds) {
            if (cardId / 13 == currentAttackCard / 13 && getCardRank(cardId) > getCardRank(currentAttackCard)) {
                if (minCardId == -1 || getCardRank(cardId) < getCardRank(minCardId)) {
                    minCardId = cardId;
                }
            }
        }
        if (minCardId == -1 && currentAttackCard / 13 != masterCard / 13) {
            for (Integer cardId : listCardIds) {
                if (cardId / 13 == masterCard / 13) {
                    if (minCardId == -1 || getCardRank(minCardId) > getCardRank(cardId)) {
                        minCardId = cardId;
                    }
                }
            }
        }
        return minCardId;
    }

    private int getCardRank(int cardId) {
        return (cardId - 1 + 13) % 13;
    }

    List<Integer> getSuggestAttackCard(List<Integer> listCardOnTable) {
        List<Integer> listSuggestCard = new LinkedList<Integer>();
        for (Integer card : listCardIds) {
            for (Integer cardOnTable : listCardOnTable) {
                if (card % 13 == cardOnTable % 13) {
                    listSuggestCard.add(card);
                    break;
                }
            }
        }
        return listSuggestCard;
    }

    int getMinCard(int masterCard) {
        int minCard = -1;
        for (Integer cardId : listCardIds) {
            if (cardId / 13 != masterCard / 13 && getCardRank(minCard) > getCardRank(cardId)) {
                minCard = cardId;
            }
        }
        if (minCard == -1) {
            for (Integer cardId : listCardIds) {
                if (getCardRank(minCard) > getCardRank(cardId)) {
                    minCard = cardId;
                }
            }
        }
        return minCard;
    }
}
